//
//  RxLearningBVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/5/7.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import CooperSwift

class RxLearningBVC: UIViewController {
    
    let disposeBag = DisposeBag()

    let label: UILabel = {
        var lb = UILabel.init()
        lb.text = "测试 binder"
        lb.font = .systemFont(ofSize: 15)
        lb.textColor = .black
        return lb
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        view.addSubviews([label])
        
        label.snp.makeConstraints { make in
            make.top.equalTo(view).offset((Screen.statusBarHeight + Screen.navBarHeight + 20))
            make.left.equalTo(view).offset(20)
            make.right.equalTo(view).offset(-20)
            make.height.equalTo(44)
        }
        
        // timer()
        PublicMethod.delay(3) { [weak self] in
            self?.binder()
        }
    }
    
    func binder() {
        /// 干活的人
        let observer : AnyObserver<Bool> = AnyObserver { (event) in
            print("observer当前线程:\(Thread.current)")
            switch event{
            case .next(let isHidden) :
                print("来了,请看label的状态")
                DispatchQueue.main.async {
                    self.label.isHidden = isHidden
                }
            case .error(let error) :
                print("\(error)")
            case .completed :
                print("完成了")
            }
        }
        observer.onNext(true)
        
        
        let binder = Binder<Bool>(self.label) { (lab, isHidden) in
            print("Binder当前线程:\(Thread.current)")
            lab.isHidden = isHidden
        }

        // 需要干的活
        let observable = Observable<Bool>.create { (ob) -> Disposable in
            ob.onNext(true)
//            ob.onError(NSError.init(domain: "com.lgcoooci.cn", code: 10086, userInfo: nil))
            ob.onCompleted()
            return Disposables.create()
        }.observe(on: ConcurrentDispatchQueueScheduler(queue: DispatchQueue.global()))
        
        // 绑定控件
        observable.bind(to: binder).disposed(by: disposeBag)
    }
    
    func timer()  {
        print("********timer********")
        //MARK:  timer
        Observable<Int>.timer(.seconds(5), period: .seconds(2), scheduler: MainScheduler.instance)
            .subscribe { (event) in
                print(event)
            }
        .disposed(by: disposeBag)

        // 因为没有指定期限period,故认定为一次性
        Observable<Int>.timer(.seconds(2), scheduler: MainScheduler.instance)
            .subscribe { (event) in
                print("111111111 \(event)")
            }
            .disposed(by: disposeBag)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
